//防止用户反复注册
var registerFlag = false;
layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','jquery','upload','laydate','baseConfig'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        laydate = layui.laydate,
        upload = layui.upload;

    //加载角色列表
    $.post( "/role/list",{},function( res){
        if( res.code == 200){
            var roles = res.data;
            var sel = $("select[name=roleId]");
            for( var i = 0 ; i < roles.length ; i++){
                var role = roles[i];
                sel.append( "<option value=\"" + role.id + "\">" + role.roleName + "</option> ")
            }
            //更新渲染
            form.render( "select");
            $("#roleSelect").val( $("#roleIdForValue").val());
            form.render( "select");
        }
    });

    //绑定校验
    form.verify({
        roleId : function(val){
            if( !val || val == ''){
                return "员工角色为必选";
            }
        },
        nom : function(val){
            if(val == ''){
                return "姓不能为空";
            }
            var rule = /^[A-Z]*$/;
            if( !rule.test( val)){
                return "姓必须全为大写字母";
            }
        },
        prenom : function(val){
            if(val == ''){
                return "名不能为空";
            }
            var rule = /^[A-Z].*$/;
            if( !rule.test( val)){
                return "名必须全为大写字母";
            }
        },
        password : function(val){
            if(val == ''){
                return "密码不能为空";
            }
            if( val != $("#rePassword").val()){
                return "两次输入的密码不相同";
            }
        },
        agreementType : function(val){
            if(val == ''){
                return "请选择合同类型";
            }
        }
    });

    //选择出生日期
    laydate.render({
        elem: '.userBirthday',
        format: 'yyyy年MM月dd日',
        trigger: 'click',
        max : 0,
        lang: 'en',
        done: function(value, date){

        }
    });

    //居留复印件 pdf 上传
    upload.render({
        elem: '#residencePdfBtn',
        url: '/file/upload?type=residencePdf',
        method : "post",
        accept: "file",
        exts: "pdf",
        done: function(res, index, upload){ //上传后的回调
            $("input[name=residencePdf]").val( res.data.filePath);
        }
    });
    //居留复印件 pdf 上传
    upload.render({
        elem: '#insurancePdfBtn',
        url: '/file/upload?type=insurancePdf',
        method : "post",
        accept: "file",
        exts: "pdf",
        done: function(res, index, upload){ //上传后的回调
            $("input[name=insurancePdf]").val( res.data.filePath);
        }
    });


    //公司证明 pdf 上传
    upload.render({
        elem: '#certificatePdfBtn',
        url: '/file/upload?type=certificatePdf',
        method : "post",
        accept: "file",
        exts: "pdf",
        done: function(res, index, upload){ //上传后的回调
            $("input[name=certificatePdf]").val( res.data.filePath);
        }
    });

    //护照证明 pdf 上传
    upload.render({
        elem: '#passportPdfBtn',
        url: '/file/upload?type=passportPdf',
        method : "post",
        accept: "file",
        exts: "pdf",
        done: function(res, index, upload){ //上传后的回调
            $("input[name=passportPdf]").val( res.data.filePath);
        }
    });

    //上传头像
    upload.render({
        elem: '.headImgBtn',
        url: '/file/upload?type=headImg',
        method : "post",
        done: function( res){
            if( res.code == 200){
                var filePath = res.data.filePath;
                $('#headImg').attr( 'src', filePath);
                $("input[name=headImg]").val( filePath);
            }
        }
    });

    //监听用户选择合同
    form.on('select(agreementTypeSelect)', function(data){
        $(".register-agreement-form").addClass( "dn");
        switch ( data.value) {
            case "1" : $("#agreement_trial").removeClass( "dn");break;
            case "2" : $("#agreement_trial").removeClass( "dn");break;
            case "3" : $("#agreement_company").removeClass( "dn");break;
            case "4" : $("#agreement_visitor").removeClass( "dn");break;
        }
    });

    //提交注册表单按钮
    form.on("submit(register)",function(data){
        //判断用户在该页面是否已经注册成功过了
        if( registerFlag){
            top.layer.alert( "你已经注册成功，请等待管理员审核帐号");
            return false;
        }
        if( !data.field.headImg){
            top.layer.alert( "请上传头像");
            return false;
        }
        if( !data.field.agreementType){
            top.layer.alert( "请选择合同类型");
            return false;
        }


        //先提交合同表单
        //根据合同类型提交对应的表单，提交接口返回合同数据的id
        var saveAgreementUrl;
        var agreementJson;
        switch ( data.field.agreementType) {
            case "1" :
                saveAgreementUrl = "/agreement/saveTrial";
                agreementJson = $("#agreement_trial").serialize();
                if( !$("input[name=birthDay]").val()){
                    top.layer.alert( "试用合同-生日为必填");
                    return;
                }
                if( !$("input[name=receiver]").val()){
                    top.layer.alert( "试用合同-收件人为必填");
                    return;
                }
                if( !$("input[name=address]").val()){
                    top.layer.alert( "试用合同-地址为必填");
                    return;
                }
                if( !$("input[name=emailCode]").val()){
                    top.layer.alert( "试用合同-邮编为必填");
                    return;
                }
                if( !$("input[name=residencePdf]").val()){
                    top.layer.alert( "试用合同-居留复印件PDF为必填");
                    return;
                }
                if( !$("input[name=residenceNo]").val()){
                    top.layer.alert( "试用合同-居留卡号为必填");
                    return;
                }
                if( !$("input[name=insurancePdf]").val()){
                    top.layer.alert( "试用合同-医保卡复印件PDF为必填");
                    return;
                }
                if( !$("input[name=insuranceNo]").val()){
                    top.layer.alert( "试用合同-医保号为必填");
                    return;
                }
                break;
            case "2" :
                saveAgreementUrl = "/agreement/saveTrial";
                agreementJson = $("#agreement_trial").serialize();
                if( !$("input[name=birthDay]").val()){
                    top.layer.alert( "CDD合同-生日为必填");
                    return;
                }
                if( !$("input[name=receiver]").val()){
                    top.layer.alert( "CDD合同-收件人为必填");
                    return;
                }
                if( !$("input[name=address]").val()){
                    top.layer.alert( "CDD合同-地址为必填");
                    return;
                }
                if( !$("input[name=emailCode]").val()){
                    top.layer.alert( "CDD合同-邮编为必填");
                    return;
                }
                if( !$("input[name=residencePdf]").val()){
                    top.layer.alert( "CDD合同-居留复印件PDF为必填");
                    return;
                }
                if( !$("input[name=residenceNo]").val()){
                    top.layer.alert( "CDD合同-居留卡号为必填");
                    return;
                }
                if( !$("input[name=insurancePdf]").val()){
                    top.layer.alert( "CDD合同-医保卡复印件PDF为必填");
                    return;
                }
                if( !$("input[name=insuranceNo]").val()){
                    top.layer.alert( "CDD合同-医保号为必填");
                    return;
                }
                break;
            case "3" :
                saveAgreementUrl = "/agreement/saveCompany";
                agreementJson = $("#agreement_company").serialize();
                if( !$("input[name=companyName]").val()){
                    top.layer.alert( "公司合同-公司名称为必填");
                    return;
                }
                if( !$("input[name=companyAddress]").val()){
                    top.layer.alert( "公司合同-公司地址为必填");
                    return;
                }
                if( !$("input[name=emailCode]").val()){
                    top.layer.alert( "公司合同-公司所在地邮编为必填");
                    return;
                }
                if( !$("input[name=certificatePdf]").val()){
                    top.layer.alert( "公司合同-公司证明为必填");
                    return;
                }
                break;
            case "4" :
                saveAgreementUrl = "/agreement/saveVisitor";
                agreementJson = $("#agreement_visitor").serialize();
                if( !$("input[name=birthDay]").val()){
                    top.layer.alert( "游客合同-生日为必填");
                    return;
                }
                if( !$("input[name=passportPdf]").val()){
                    top.layer.alert( "游客合同-护照PDF为必填");
                    return;
                }
                if( !$("input[name=passportNo]").val()){
                    top.layer.alert( "游客合同-护照号为必填");
                    return;
                }
                break;
        }
        $.post( saveAgreementUrl, agreementJson , function( res){
            if( res.code == 200){
                var agreementId = res.data;
                $.post("/adminUser/regist",{
                    'id': data.field.id,
                    'phone': data.field.phone,
                    'email': data.field.email,
                    'headImg': data.field.headImg,
                    'nom': data.field.nom,
                    'prenom': data.field.prenom,
                    'password': hex_md5( data.field.password),
                    'roleId' : data.field.roleId,
                    'agreementType' : data.field.agreementType,
                    'agreementId' : agreementId
                },function( res){
                    if( res.code == 200){
                        registerFlag = true;
                        top.layer.alert( res.msg);
                    }else{
                        top.layer.alert( res.msg);
                    }
                    return false;
                });
            }else{
                top.layer.alert( res.msg);
                return false;
            }
        });
    });

    $("#goLoginBtn").click( function () {
        window.location.href = "/pages/login.html";
    });
});
